﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;

namespace BGShell.Components
{
    [ProvideProperty("FullScreen", typeof(Form))]
    [System.ComponentModel.DesignerCategory("Code")]
    internal sealed class FullScreenViewProvider : Component, IExtenderProvider
    {
        private Dictionary<Form, FormInfo> _forms;

        [DefaultValue(false)]
        public bool GetFullScreen(Form form)
        {
            if (_forms != null)
            {
                FormInfo result;

                if (_forms.TryGetValue(form, out result))
                {
                    return result.FullScreen;
                }
            }

            return false;
        }

        public void SetFullScreen(Form form, Boolean value)
        {
            if (GetFullScreen(form) != value)
            {
                if (_forms == null)
                {
                    _forms = new Dictionary<Form, FormInfo>();
                }

                FormInfo info;

                if (!_forms.TryGetValue(form, out info))
                {
                    _forms[form] = info = new FormInfo(form);
                }

                info.FullScreen = value;
            }
        }

        bool IExtenderProvider.CanExtend(object extendee)
        {
            return extendee is Form;
        }

        private sealed class FormInfo
        {
            private readonly Form _form;

            private Rectangle _bounds;
            private FormBorderStyle _border;
            private FormWindowState _windowState;
            
            private Boolean _fullScreen;

            public FormInfo(Form form)
            {
                _form = form;
            }

            public bool FullScreen
            {
                get { return _fullScreen; }
                set
                {
                    if (_fullScreen != value)
                    {
                        _fullScreen = value;

                        if (value)
                        {
                            _border = _form.FormBorderStyle;
                            _windowState = _form.WindowState;

                            if (_windowState == FormWindowState.Maximized)
                            {
                                _bounds = _form.RestoreBounds;
                            }
                            else
                            {
                                _bounds = _form.Bounds;
                            }

                            _form.FormBorderStyle = FormBorderStyle.None;
                            _form.WindowState = FormWindowState.Normal;
                            _form.Bounds = Screen.GetBounds(_form);

                            _form.BringToFront();
                        }
                        else
                        {
                            _form.FormBorderStyle = _border;
                            _form.Bounds = _bounds;

                            _form.WindowState = _windowState;
                        }
                    }
                }
            }
        }
    }
}
